import allure
from appium.webdriver.common.appiumby import AppiumBy

from .base import Base


class Optional_page(Base):  # 自选页
    # 全部
    __ALL = (AppiumBy.XPATH, '//android.widget.TextView[@text="全部"]')
    # 恢复
    __RECOVER = (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.xueqiu.android:id/restore_default" and @text="恢复"]')
    # 价格降序
    __DESCENDING_ORDER = (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.xueqiu.android:id/item_layout" and @text="最新价"]')
    # 价格
    __PRICE = (AppiumBy.XPATH, '//*[@resource-id="com.xueqiu.android:id/content_recycler"]/*/*[2]/*[2]/*/*[1]/android.widget.TextView[1]')
    # 设置
    __SETTING = (AppiumBy.ID, 'com.xueqiu.android:id/portfolio_setting')
    # 股票指标
    __STOCK_INDEX = (AppiumBy.ID, 'com.xueqiu.android:id/stock_index_setting')
    # 设置->股票指标 标题
    __SETTING_STOCK_INDEX_TITLE = (AppiumBy.ID, 'com.xueqiu.android:id/action_title')

    def go_all(self):
        with allure.step("去全部"):
            self.find_click(self.__ALL)
            return Optional_page(self.driver)

    def get_all_price(self, img_name, descending_order=None):     # 获取股票价格
        self.implicitly_wait(0.3)
        if self.find(self.__RECOVER):
            self.find_click(self.__RECOVER)
        self.implicitly_wait(10)

        with allure.step("获取股票价格"):
            if descending_order:    # 是否降序
                self.find_click(self.__DESCENDING_ORDER)

            list_price = []  # 存储股票价格
            price = self.finds(self.__PRICE)
            for i in range(len(price)):
                price_text = float(price[i].text)
                list_price.append(price_text)

            self.img_log(img_name, "股票价格")
            return list_price   # 返回股票价格

    def go_setting_stock_index(self, img_name, duration=1000):
        with allure.step("进入设置中的股票指标中"):
            self.find_click(self.__SETTING)     # 进入设置
            self.swip_by(self.__STOCK_INDEX, duration=duration)     # 滑动查找股票指标
            self.find_click(self.__STOCK_INDEX)     # 点击股票指标
            title = self.find_text(self.__SETTING_STOCK_INDEX_TITLE)    # 获取股票指标标题
            self.img_log(img_name, "股票指标")
            return title    # 返回股票指标标题
